package com.eig.course.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.entity.course.EduSubject;
import com.eig.common.vo.Result;
import com.eig.course.dao.CoreDao;
import com.eig.course.dao.CoreVoDao;
import com.eig.course.service.CoreService;

import com.eig.course.utils.IdWorker;
import com.eig.course.vo.EduSubjectVo;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2022/12/07 下午 03:08
 * @description： 课程分类
 * @modified By：
 * @version:
 */
@Service
public class CoreServiceImpl implements CoreService {

    @Resource
    private CoreDao coreDao;

    @Resource
    private IdWorker idWorker;

    @Resource
    private CoreVoDao coreVoDao;

    List<String> list = new ArrayList<>();

    @Override
    public Result findsubject(PageRequest pageRequest, EduSubject exception) {
        long total = this.coreDao.count(exception);
        PageImpl<EduSubject> eduSubjectsL = new PageImpl<>(this.coreDao.queryAllByLimit(exception, pageRequest), pageRequest, total);
        LinkedList<EduSubject> children = null;
        List<EduSubject> fafter = null;
        if (ObjectUtil.isNotEmpty(eduSubjectsL.getContent())) {
            //子菜单
            exception.setTitle("");
            exception.setStatusId("");
            children = coreDao.queryAll(exception);
            fafter = eduSubjectsL.getContent();
        } else {
            //子菜单
            children = coreDao.queryAll(exception);
            exception.setTitle("");
            exception.setStatusId("");
            eduSubjectsL = new PageImpl<>(this.coreDao.queryAllByLimit(exception, pageRequest), pageRequest, total);
            fafter = eduSubjectsL.getContent();
            for (int i = 0; i < fafter.size(); i++) {
                for (EduSubject subject : children) {
                    if (fafter.get(i).getId().equals(subject.getParentId())) {
                        fafter.get(i).setChildren(children);
                        list.add(fafter.get(i).getId());
                    }
                }
            }
            for (String s : list) {
                if (ObjectUtil.isNotEmpty(s)) {
                    fafter = fafter.stream().filter(index -> index.getId() == s).collect(Collectors.toList());
                }
            }
//            if (ObjectUtil.isEmpty(fafter)) {
//                System.out.println("=====");
//            }
        }

        //一级菜单
        List<EduSubject> eduSubjects = fromPermission(fafter, children);

        Map<String, Object> map = new HashMap();
        map.put("records", eduSubjects);
        map.put("total", eduSubjectsL.getTotalElements());
        return new Result(2000, "查询成功", map);
    }


    @Override
    public Result insertsubject(EduSubject eduSubject) {
        if (eduSubject.getId().equals("0")) {
            eduSubject.setId(idWorker.nextId());
            eduSubject.setParentId("0");
        } else {
            eduSubject.setParentId(eduSubject.getId());
            eduSubject.setId(idWorker.nextId());
            System.out.println(eduSubject.getId());

        }
        eduSubject.setStatusId("1");
        eduSubject.setSort(0);
        int insert = coreDao.insertsubject(eduSubject);
        if (insert > 0) {
            return new Result(2000, "添加成功");
        }
        return new Result(5000, "添加失败");
    }

    @Override
    public Result updatesubject(EduSubjectVo eduSubject) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", eduSubject.getId());
        updateWrapper.set("title", eduSubject.getTitle());
        updateWrapper.set("remark", eduSubject.getRemark());
        int update = coreVoDao.update(eduSubject, updateWrapper);
        if (update > 0) {
            return new Result(2000, "修改成功");
        }
        return new Result(5000, "修改失败");
    }

    @Override
    @Transactional
    public Result changeStatusId(EduSubject eduSubject) {
        if (eduSubject.getParentId().equals("0")) {
            coreDao.changeStatusId(eduSubject);
            coreDao.changeStatusIdById(eduSubject);
        } else {
            coreDao.changeStatusId(eduSubject);
        }
        return new Result(2000, "修改成功");
    }

    private List<EduSubject> fromPermission(List<EduSubject> fullName, List<EduSubject> children) {
        List<EduSubject> arrayList = new ArrayList<>();
        //遍历所有menu对象，判断是否有孩子持续递归
        for (EduSubject permission : fullName) {
            //设置子目录
            EduSubject treeChildren = setTreeChildren(permission, children);
            arrayList.add(treeChildren);
        }
        return arrayList;
    }

    private EduSubject setTreeChildren(EduSubject permis, List<EduSubject> children) {
        //此集合封装所有孩子
        List<EduSubject> childrenAll = new ArrayList<>();
        for (EduSubject aclPermission : children) {
            if (aclPermission.getParentId().equals(permis.getId())) {
                //递归设置子菜单
                EduSubject treeChildren = setTreeChildren(aclPermission, children);
                childrenAll.add(treeChildren);
            }
        }
        permis.setChildren(childrenAll);
        return permis;
    }
}
